/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.ufrn.aipetro.ga.view.search;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;

import br.com.ufrn.aipetro.ga.domain.entity.plant.Plant;
import br.com.ufrn.aipetro.ga.domain.entity.unit.Unit;
import br.com.ufrn.aipetro.ga.model.persist.hibernate.util.HibernateUtil;

/**
 *
 * @author Andre
 */
public class SearchPlantController implements Initializable {

	Session session;
	@FXML private TextField tfdSearchParam;
	@FXML private TableView<Plant> tblSearchResult;
	private TableColumn<Plant, String> tblSearchResultNameCol;
	private ObservableList<Plant> tblSearchResultData;
	
    @Override
    public void initialize(URL url, ResourceBundle rb) {
//        throw new UnsupportedOperationException("Not supported yet.");
    	configureResultTable();
    }
    
    private void configureResultTable() {
    	tblSearchResultNameCol = new TableColumn<Plant, String>("Nome da Planta");
    	tblSearchResultNameCol.setCellValueFactory(new PropertyValueFactory<Plant,String>("name"));
    	
    	tblSearchResult.getColumns().clear();
    	tblSearchResult.getColumns().add(tblSearchResultNameCol);
    	
    	tblSearchResultData = FXCollections.observableArrayList();
    	tblSearchResult.setItems(tblSearchResultData);
    }
    
    @FXML
    public void search() {
    	session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		Plant plant = new Plant();
		plant.setName(tfdSearchParam.getText());
		Example example = Example.create(plant)
				.enableLike(MatchMode.START);
		
		List<Plant> plantList = (List<Plant>)session.createCriteria(Plant.class)
				.add(example)
				.list();
		session.getTransaction().commit();
		
		tblSearchResult.getItems().clear();
		tblSearchResult.getItems().addAll(plantList);
    }
    
    @FXML
    public void detail() throws IOException {
//    	FXMLLoader loader = new FXMLLoader(getClass().getResource("PlantFXML.fxml"));
//		Parent root = (Parent) loader.load();
//		
//		Scene scene = new Scene(root);
//		Stage stage = new Stage();
//		stage.setScene(scene);
//		
//		FormPlantFxmlController plantController = (FormPlantFxmlController)loader.getController();
//		plantController.setPlant(tblSearchResult.getSelectionModel().getSelectedItem());
//		plantController.setPrimaryStage(stage);
//		
//		stage.show();
    }
    
    @FXML
    public void delete() {
    	session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.delete(tblSearchResult.getSelectionModel().getSelectedItem());
		session.getTransaction().commit();
		search();
    }
}
